package com.itheima.web;

import com.itheima.Service.UserService;
import com.itheima.dao.pojo.User;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/deleteUserById")
public class DeleteUserByIDServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取传递过来的数据
        Map<String, String[]> map = request.getParameterMap();
        //创建一个user对象
        User user = new User();
        //调用方法
        UserService userService = new UserService();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        boolean b = userService.deleteUser(user);
        if (b){
            request.getRequestDispatcher("delete_ok.jsp").forward(request,response);
        }else {
            request.getRequestDispatcher("delete_sb.jsp").forward(request,response);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
